package com.liwen.service.depend;

import com.alibaba.fastjson.JSON;
import com.liwen.dao.entity.OrderEntity;
import com.liwen.dao.mapper.OrderMapper;
import com.liwen.service.task.*;
import com.liwen.service.task.model.TaskParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@Service
public class OrderService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private OrderMapper orderMapper;

    public OrderEntity bysn(int insId, String sn) {
        return orderMapper.bySn(insId, sn);
    }

    public void testForkJoinPool() {
        TaskParam param = new TaskParam();
        MultiTablesQuery multiTablesQuery = new MultiTablesQuery() {
            @Override
            public void query(int suffix, TaskParam taskParam) {
                log.info("第" + suffix + "任务：参数：param：" + JSON.toJSONString(taskParam));
            }
        };
        ForkJoinPool forkJoinPool = ForkJoinPoolSingleton.getInstance();
        MultiTablesQueryTask task = new MultiTablesQueryTask(null, 64, param, multiTablesQuery);
        forkJoinPool.invoke(task);
    }

    public List<OrderEntity> testForkJoinPool2() {
        TaskParam param = new TaskParam();
        MultiTablesListQuery<OrderEntity> multiTablesListQuery = new MultiTablesListQuery() {
            @Override
            public List<OrderEntity> query(int suffix, TaskParam taskParam) {
                log.info("第" + suffix + "任务：参数：param：" + JSON.toJSONString(taskParam));
                //return order().findOrder(suffix, taskParam);
                return null;
            }
        };
        ForkJoinPool forkJoinPool = ForkJoinPoolSingleton.getInstance();
        MultiTablesListQueryTask<OrderEntity> task = new MultiTablesListQueryTask(64, null, param, multiTablesListQuery);
        List<OrderEntity> orderEntityList = forkJoinPool.invoke(task);//获取64张表所有满足的订单
        return orderEntityList;
    }
}
